package com.ruoyi.web.controller.common;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.mytools.Tools;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Articles;
import com.ruoyi.system.domain.BankObject;
import com.ruoyi.system.domain.Pictures;
import com.ruoyi.system.domain.Videos;
import com.ruoyi.system.service.IArticlesService;
import com.ruoyi.system.service.IPicturesService;
import com.ruoyi.system.service.IVideosService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@Slf4j
@RequestMapping("/")
public class indexController extends BaseController {

    @Resource
    private IArticlesService articlesService;

    @Resource
    private IPicturesService picturesService;
    @Resource
    private IVideosService videosService;


    @GetMapping(value = {"/","/index","/index.html"})
    public String index(Model model) {

        model.addAttribute("pageTitle","活动公告");
        model.addAttribute("pageDescription","，活动公告");
        model.addAttribute("pageKeywords","活动公告");


        String hostIP="http://bank.huajvn.com";
        //String hostIP="http://localhost:9090";

        List<BankObject> BankList=new ArrayList<>();
        String[] imgs=new String[17];
        for (int i = 0; i < 25; i++) {
            BankObject bankObject=new BankObject();
            int num=i+1;
            //bankObject.setImgUrl("/images/bankimgs/"+num+".jpg");
            //设置页面封面图
            if(num==9) continue;
            bankObject.setImgUrl("https://huajvn.oss-cn-zhangjiakou.aliyuncs.com/images/bank3/" + num + ".jpg");
            bankObject.setImgLink("");
            BankList.add(bankObject);
        }

        /*
        System.out.println("BankList===>");
        BankList.forEach(System.out::println);
        //*/

        //http://bank.huajvn.com/pages/50ss.jpg

        //设置跳转链接
        //0不设置，从1开始,第一张图0不需要链接
        BankList.get(1).setImgLink(hostIP+"/pages/2-2.jpg");
        BankList.get(2).setImgLink("/pages/3page.jpg");
        BankList.get(3).setImgLink("https://mp.weixin.qq.com/s/9b0K4vvWy7E4dZxpeKx3Gg");
        BankList.get(4).setImgLink(hostIP+"/pages/xykhk.jpg");
        BankList.get(5).setImgLink(hostIP+"/pages/ysyjc.jpg");
        BankList.get(6).setImgLink(hostIP+"/pages/szmyt.jpg");
        BankList.get(7).setImgLink(hostIP+"/pages/z6kh.jpg");
        //BankList.get(8).setImgLink(hostIP+"/pages/xfnh.jpg");
        BankList.get(8).setImgLink(hostIP+"/pages/jiayou3.jpg");
        BankList.get(9).setImgLink("https://mp.weixin.qq.com/s/dhc9xsqRlFiyosO6C9oNvQ");
        //BankList.get(10).setImgLink("/pages/czyhjxlj.jpg");
        BankList.get(10).setImgLink("/pages/czyhjxlj.jpg");
        BankList.get(11).setImgLink("https://mp.weixin.qq.com/s/t6YwbKnueWo_ZQiCNqGJSg");
        BankList.get(12).setImgLink("http://fenqi.oksheji.cn/");
        BankList.get(13).setImgLink("https://mp.weixin.qq.com/s/e8Llf1n91s8nw7Drh52VuA");
        BankList.get(14).setImgLink(hostIP+"/pages/yjhx.jpg"); //以旧换新

        BankList.get(15).setImgLink(hostIP+"/pages/wanda.jpg"); //以旧换新
        BankList.get(16).setImgLink(hostIP+"/pages/drf.jpg");
        BankList.get(17).setImgLink(hostIP+"/pages/mxbc.jpg");
        BankList.get(18).setImgLink(hostIP+"/pages/zfb.jpg");
        BankList.get(19).setImgLink(hostIP+"/pages/taobao.jpg");
        BankList.get(20).setImgLink(hostIP+"/pages/12306.jpg");
        BankList.get(21).setImgLink(hostIP+"/pages/douyin.jpg");
        BankList.get(22).setImgLink(hostIP+"/pages/pdd.jpg");
        BankList.get(23).setImgLink(hostIP+"/pages/25page.jpg");
        //https://mp.weixin.qq.com/s/t6YwbKnueWo_ZQiCNqGJSg 轻松省一笔
        /*
        BankList.get(3).setImgLink(hostIP+"/pages/b3.jpg"); //徐闻商圈，万达商圈
        BankList.get(4).setImgLink(hostIP+"/pages/b4.jpg"); //越刷越精彩
        BankList.get(5).setImgLink(hostIP+"/pages/b5.jpg");//1000
        BankList.get(6).setImgLink(hostIP+"/pages/b6.jpg"); //首刷50
        BankList.get(7).setImgLink(hostIP+"/pages/b7.jpg");
        BankList.get(8).setImgLink(hostIP+"/pages/b8.jpg"); //美宜佳
        BankList.get(12).setImgLink(hostIP+"/pages/b12.jpg"); //美好生活
        //*/

        model.addAttribute("BankList",BankList);

        return "index";
    }



    @GetMapping(value = {"/pages/{pagename}"})
    public String pages(Model model,@PathVariable(value = "pagename",required = true) String pagename) {
        System.out.println(pagename);
        model.addAttribute("pageTitle","活动详情");
        model.addAttribute("pageDescription","，活动详情");
        model.addAttribute("pageKeywords","活动详情");


        List<String> pageListname=new ArrayList<>();
        String http="https://huajvn.oss-cn-zhangjiakou.aliyuncs.com/images/bank3/";

        if(pagename.indexOf(",")!=-1){
            //多个链接后缀名称
            String[] templinks=pagename.split(",");
            for (int i = 0; i < templinks.length; i++) {
                pageListname.add(http+templinks[i]);
            }
        }else{
            pageListname.add(http+pagename); //单个链接名称
        }

        model.addAttribute("pageListname",pageListname);

        return "pages";
    }




    @GetMapping(value = {"/detail","/detail.html","/detail_{id}.html","/detail/{tag}/{mode}.html","/detail/{tag}/{mode}/{pnums}.html"})
    //public String detail(Model model, @PathVariable(value = "id",required = false) Long id, @PathVariable(value = "mode",required = false) String mode) {
    public String detail(
            Model model,
            @PathVariable(value = "id",required = false) Long id,
            @PathVariable(value = "tag",required = false) String tag,
            @PathVariable(value = "mode",required = false) String mode,
            @PathVariable(value = "pnums",required = false) String pnums){

        String tagValue="";
        int pageNum=1;
        int pageSize=2;
        int curPage=1;

        model.addAttribute("pageTitle","湛江玛丽亚后台管理系统 - 湛江政府采购|工程招标|招标代理机构 - 官方网站");
        model.addAttribute("pageDescription","湛江玛丽亚后台管理系统，是专业从事政府采购、工程招标、企业招标、招标信息咨询、合同审查及项目验收的综合性招标代理服务企业");
        model.addAttribute("pageKeywords","湛江玛丽亚后台管理系统");

        if(!Objects.isNull(id)){
            System.out.println("访问的id:==>"+id);
            Articles articles=articlesService.selectArticlesById(id);
            System.out.println("输出文章"+articles);
            model.addAttribute("articlesData",articles);
            model.addAttribute("dataid",id);

            if(!StringUtils.isEmpty(articles.getAttachments())){
                List<String> attachmentsList=new ArrayList<>();
                String[] Arrs=articles.getAttachments().replace("[","").replace("]","").split(",");
                for (int i = 0; i < Arrs.length; i++) {
                    attachmentsList.add(Arrs[i]);
                }
                System.out.println("附件列表："+attachmentsList);
                model.addAttribute("attachmentsList",attachmentsList);
            }else{
                model.addAttribute("attachmentsList",null);
            }

        }

        if(!Objects.isNull(tag)){

            System.out.println("Tag标签:==>"+tag);
            if(tag.equals("zhaobiao")) tagValue="招标公告";
            if(tag.equals("zhongbiao")) tagValue="中标公示";
            if(tag.equals("gengzheng")) tagValue="更正公告";
            if(tag.equals("zixundongtai")) tagValue="资讯动态";
            if(tag.equals("zhengce")) tagValue="政策法规";
            if(tag.equals("success")) tagValue="成功案例";
            if(tag.equals("download")) tagValue="资料下载";
            System.out.println("读取"+tagValue+"...");

            //如果分页不为空
            //控制分页都在TableDataInfo数据集里，getTotal获取
            //在前端里分页显示控制
            if(!Objects.isNull(pnums)){
                pageNum=Integer.valueOf(pnums);
                curPage=pageNum;
            }

            Articles articles=new Articles();
            articles.setAtype(tagValue); //查询的分类  【资讯动态】/【资料下载】


            //包装分页属性
            TableDataInfo articlesListData=list(pageNum,pageSize,articles);
            System.out.println("分页列表");
            System.out.println(articlesListData);

            System.out.println("循环分页列表.........................");
            /*
                articlesListData.getRows().forEach(System.out::println);
                System.out.println("总数.........................");
                System.out.println(articlesListData.getTotal());
                System.out.println("页数.........................");
                //System.out.println(articlesListData.getTotal()/pageSize);
                System.out.println(25/pageSize);
                System.out.println("是否有余.........................");
                System.out.println(25%pageSize);
                System.out.println(30%pageSize);
                System.out.println(20%pageSize);
                System.out.println(21%pageSize);
            //*/

            Long dataPage=articlesListData.getTotal()/pageSize;
            boolean isYu=articlesListData.getTotal()%pageSize>0?true:false; //是否有余数;
            dataPage=isYu==true?dataPage+1:dataPage;
            System.out.println("分页数:"+dataPage);

            //测试分页
            //dataPage=30L;

            model.addAttribute("articlesListData",articlesListData.getRows());
            model.addAttribute("dataTotal",articlesListData.getTotal());

            List<Integer> items =new ArrayList<>();
            for (Integer i = 0; i < dataPage; i++) {
                items.add(i+1);
            }
            model.addAttribute("PageTotal",items);
            model.addAttribute("PageCount",items.size());
            model.addAttribute("PageTag",tag);
            model.addAttribute("curPage",curPage);
            model.addAttribute("tagValue",tagValue);
        }

        //单例模式 or 列表模式
        if(!Objects.isNull(mode)){
            model.addAttribute("mode",mode);
        }else{
            model.addAttribute("mode",null);
        }
        return "detail";
    }


    //分页列表
    private TableDataInfo list(Integer pNum,Integer pSize,Articles articles)
    {
        startPage(pNum,pSize);
        List<Articles> list = articlesService.selectArticlesList(articles);
        return getDataTable(list);
    }


}
